package com.jlnk.androidfmc.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.jlnk.androidfmc.MainActivity;
import com.jlnk.androidfmc.R;
import com.jlnk.androidfmc.view.ConsultDetail;

public class ConsultFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final int YOUR_REQUEST_CODE = 100;

    private String mParam1;
    private String mParam2;

    public ConsultFragment() {
        // Required empty public constructor
    }

    public static ConsultFragment newInstance(String param1, String param2) {
        ConsultFragment fragment = new ConsultFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_consult, container, false); // 确保布局文件名正确

        // 初始化按钮并设置监听器
        // 假设您已经按照专家信息的数量设置了按钮的ID
        Button consultExpertButton1 = view.findViewById(R.id.consultExpertButton1);
        Button consultExpertButton2 = view.findViewById(R.id.consultExpertButton2);
        Button consultExpertButton3 = view.findViewById(R.id.consultExpertButton3);
        Button consultExpertButton4 = view.findViewById(R.id.consultExpertButton4);
        Button consultExpertButton5 = view.findViewById(R.id.consultExpertButton5);
        // ... 为其他按钮也进行类似的初始化

        // 设置按钮的点击事件
        consultExpertButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 使用 YOUR_REQUEST_CODE 作为请求码启动 ConsultDetail
                Intent intent = new Intent(getActivity(), ConsultDetail.class);
                intent.putExtra("EXPERT_NAME", "薛亚丽"); // 传递专家名称
                // 确保 YOUR_REQUEST_CODE 是一个您定义的常量
                startActivityForResult(intent, YOUR_REQUEST_CODE);
            }
        });


        consultExpertButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ConsultDetail.class);
                intent.putExtra("EXPERT_NAME", "赵一博"); // 传递第二个专家的名称
                startActivityForResult(intent, YOUR_REQUEST_CODE);
            }
        });

        consultExpertButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ConsultDetail.class);
                intent.putExtra("EXPERT_NAME", "何浩楠"); // 传递第三个专家的名称
                startActivityForResult(intent, YOUR_REQUEST_CODE);
            }
        });

        consultExpertButton4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ConsultDetail.class);
                intent.putExtra("EXPERT_NAME", "赵小童"); // 传递第三个专家的名称
                startActivityForResult(intent, YOUR_REQUEST_CODE);
            }
        });

        consultExpertButton5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ConsultDetail.class);
                intent.putExtra("EXPERT_NAME", "陈少熙"); // 传递第三个专家的名称
                startActivityForResult(intent, YOUR_REQUEST_CODE);
            }
        });


        return view;
    }

    // 这里可以处理从 ConsultDetail 返回的结果
// ConsultFragment.java

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == YOUR_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
            String consultationContent = data.getStringExtra("CONSULTATION_CONTENT");
            // 此处我们直接调用 MainActivity 的方法来更新 MessageFragment
            MainActivity mainActivity = (MainActivity) getActivity();
            if (mainActivity != null) {
                mainActivity.displayConsultationInfo("专家姓名", consultationContent);
            }
        }
    }
}